home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 1.iso
/
ARGONET
/
PD
/
GAMES
/
WIMPGAME
/
MINES2.ZIP
/
!Mines
/
c
/
Mines
< prev
next >
Wrap
Text File
|
1995-05-01
|
28KB
|
847 lines
/* Mines
Diese wunderbare Version wurde geschrieben von:
Michael Dreyer
Tycho-Brahe-Weg 44b
22043 Hamburg
Germany
Email: dreyer@physnet.uni-hamburg.de
Have much fun ! */
/* Einige lustige includes ... */
#include "wimp.h" /* access to WIMP SWIs */
#include "colourtran.h" /* interface to colour translation module */
#include "os.h" /* low-level RISCOS access */
#include "alarm.h"
#include "mouse.h"
#include "osspriteop.h"
#include "osword.h"
/* ... und noch ein paar ... */
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#define VERSION "2.00 31.4.1995" /* Momentane Versionsnummer (Wird ständig geändert) */
#define INFO 0
#define LEV1 1
#define LEV2 2
#define LEV3 3
#define LEV4 4
#define CUSTOM 5
#define HINT 6
#define NEU 7
#define QUIT 8
#define MINE 1 /* Flags für die Felder */
#define MARK 2
#define CLOSED 4
/* einige nützliche Definitionen */
#define SpriteSize 2000+X_SIZE*Y_SIZE/2+sizeof(osspriteop_header)+sizeof(osspriteop_area)
#define MODE 12
#define X_SIZE 610
#define Y_SIZE 192
#define TickMenu(m,n) (m.entries[n].menu_flags |= wimp_MENU_TICKED)
#define UntickMenu(m,n) (m.entries[n].menu_flags &= ~wimp_MENU_TICKED)
#define ToggleTickMenu(m,n) (m.entries[n].menu_flags ^= wimp_MENU_TICKED)
#define ShadeMenu(m,n) (m.entries[n].icon_flags |= wimp_ICON_SHADED)
#define UnshadeMenu(m,n) (m.entries[n].icon_flags &= ~wimp_ICON_SHADED)
#define ToggleShadeMenu(m,n) (m.entries[n].icon_flags ^= wimp_ICON_SHADED)
typedef struct
{ osspriteop_area *area;
osspriteop_id id;
} spr_details;
typedef struct {wimp_w w ;
wimp_window *window ;
char *font ;
char *buffer ;
}window_data ;
typedef int BOOL ;
#define HEIGHT 192 /* wird möglicherweise mal variabel */
#define WEIDTH 610
#define XOFF 5
#define YOFF 3
#define XMAX 30
#define YMAX 16
/* My Icon Flags */
#define MIF (wimp_ICON_TEXT|wimp_ICON_VCENTRED|(7<<wimp_ICON_FG_COLOUR_SHIFT))
#include "Hint.h"
static window_data mainwindow = {0,0,0,0} ; /* DAS Spielfenster */
static window_data winwindow = {0,0,0,0}; /* Das Fenster zum Gewinnen */
static window_data infowindow = {0,0,0,0};
static wimp_MENU(3) cusmenu = {{'M','i','n','e','s',0,0,0,0,0,0,0},
7,2,7,0,150,36,4,
{{wimp_MENU_WRITABLE|wimp_MENU_LAST,(wimp_menu*)-1,
wimp_ICON_INDIRECTED|MIF,{0,0,0,0,0,0,0,0,0,0,0,0}}}} ;
/* das Hauptmenu */
static wimp_MENU(9) mainmenu = {{'M','i','n','e',' ','M','e','n','u',0,0,0},
7,2,7,0,150,36,4,
{{wimp_MENU_SEPARATE,(wimp_menu*)-1,MIF,
{'I','n','f','o',0,0,0,0,0,0,0,0}},
{0,(wimp_menu*)-1,MIF,
{'B','e','g','i','n','n','e','r',0,0,0,0}},
{0,(wimp_menu*)-1,MIF,
{'N','o','v','i','c','e',0,0,0,0,0,0}},
{0,(wimp_menu*)-1,MIF,
{'E','x','p','e','r','t',0,0,0,0,0,0}},
{0,(wimp_menu*)-1,MIF,
{'M','a','s','t','e','r',0,0,0,0,0,0}},
{wimp_MENU_SEPARATE,(wimp_menu*)&cusmenu,MIF,
{'C','u','s','t','o','m',0,0,0,0,0,0}},
{0,(wimp_menu*)-1,MIF,
{'H','i','n','t',0,0,0,0,0,0,0,0}},
{wimp_MENU_SEPARATE,(wimp_menu*)-1,MIF,
{'N','e','w',' ','G','a','m','e',0,0,0,0}},
{wimp_MENU_LAST,(wimp_menu*)-1,MIF,
{'Q','u','i','t',0,0,0,0,0,0,0,0}}}
} ;
static spr_details my_sprite ; /* Fenster Sprite */
static spr_details open[9] ; /* Zahlen auf dem Spielfeld */
static char *openstr[9] = {"0","1","2","3","4","5","6","7","8"} ;
static spr_details playground ; /* Spielfeld */
int feld[XMAX][YMAX] ; /* DAS Spielfeld */
spr_details mark ; /* Markiertes Feld */
spr_details cover; /* Normal Bedecktes Feld */
spr_details mine ; /* Mine */
spr_details nomine ; /* doch keine Mine */
spr_details questionmark; /* ein Fragezeichen */
spr_details barsprite ;
static spr_details number[11] ; /* Zahlen zur Anzeige */
static char *numberstr[11] = {"nleer","n0","n1","n2","n3","n4","n5","n6","n7","n8","n9"} ;
static BOOL displaying = FALSE; /* window on display? */
/* =-1 : Game Over
sonst Bit 0 : player 0,1
1 : =1 : Droping
*/
int mines_left = -1,fields_left = 0; /* Zähler Variable */
/* Richtungen zum Suchen */
int off[8][2] = { {-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{-1,1},{0,1},{1,1}};
static int level = 0,anzahl[4] = {30,60,90,120} ; /* Level Daten */
static osspriteop_trans_tab *trans; /* colour translation table */
static char cusstr[4] ; /* Speicher fuer das Custom Menu */
static wimp_t I_am ;
static int This_Version ;
static wimp_i baricon ;
void res_feld(void) ; /* alle Header */
void redraw_window(wimp_w handle) ;
void update_window(wimp_draw r) ;
void redo_window(wimp_draw r, BOOL more) ;
void open_window(wimp_open *o);
void icon_links(void) ;
void mine_handler(void) ;
void create_sprite(void);
static BOOL main_init(void);
void res_sprite(void);
void click_on(int x,int y, bits button ) ;
void open_field(int x,int y) ;
void show_all(void) ;
int count(int x,int y) ;
int tidy_up(int x,int y) ;
int no_error(void);
void win(void) ;
void close_win_window(void);
void show_anzahl(void) ;
void blotch(void);
void werr(os_error *err)
{if(err != 0)
{wimp_report_error(err,2,"!Test") ;
exit(1) ;
}
}
void loosewin(window_data *w)
{int i,j,h ;
if(w->w != 0)
{while((j=w->font[i++])>0)
{h = w->font[i++] ;
while(h-->0)
werr(xfont_lose_font(j)) ;
}
free(w->buffer) ;
free(w->font) ;
free(w->window) ;
w->w = 0 ;
}
}
void loadwin(window_data *w,char *s)
{char win[13] ;
int i,j,k ;
font_f font[256] ;
strncpy(win,s,12);
win[13] = 0 ;
werr(xwimp_load_template(0,0,0,font,win,0,&i,&j,&k)) ;
w->window = (wimp_window*)malloc(i+j) ;
if( j>0) w->buffer = malloc(j) ;
else w->buffer = 0 ;
for(i=0;i<256;i++) font[i]=0;
werr(xwimp_load_template(w->window,w->buffer,w->buffer+j,font,win,0,&i,&j,&k));
j = 0 ;
for(i=0;i<256;i++) if(font[i]) j++ ;
w->font = (char*) malloc(j*2+2) ;
j = 0 ;
for(i=0;i<256;i++) if(font[i]) {w->font[j++] = i ; w->font[j++] = font[i];}
w->font[j++] = 0 ;
w->font[j++] = 0 ;
}
/* Feder initialisieren (feld,bottom,vlines,lines)*/
void res_feld()
{int i,j ;
div_t r ;
if(level == 4) /* Zahl der Minen suchen */
{sscanf(cusstr,"%d",&mines_left) ;
if(mines_left < 30) mines_left = 30 ; /* Minimum */
if(mines_left > XMAX*YMAX*90/100) mines_left = XMAX*YMAX*90/100; /* Maximum 90% */
}
else
mines_left = anzahl[level] ;
sprintf(cusstr,"%d",mines_left) ;/* Minenzahl ins Custom-Menu */
fields_left = XMAX*YMAX-mines_left ;/* Freie Felder */
for( i = 0 ; i < XMAX ; i++ ) /* alle Felder ohne Mienen und geschlossen */
for( j = 0 ; j < YMAX ; j++ )
feld[ i ][ j ] = CLOSED ;
for( i=0 ; i<mines_left ;i++) /* jetzt ein paar Minen legen */
{do{r = div( rand() , XMAX ) ;
j = r.rem ;
r = div( rand() , YMAX ) ;
}while(feld[j][r.rem] & MINE) ;
feld[j][r.rem] |= MINE ;
}
}
/* Sprite zurücksetzen */
void res_sprite(void)
{osspriteop_save_area *old;
int i,j,s1,s2,s3,s4;
/* alles in mein Sprite */
old = (osspriteop_save_area *)malloc(osspriteop_read_save_area_size(osspriteop_PTR,my_sprite.area,my_sprite.id)) ;
old->a[0] = 0 ;
werr(xosspriteop_switch_output_to_sprite(osspriteop_PTR,my_sprite.area,my_sprite.id,old,&s1,&s2,&s3,&s4));
/* Feld plotten */
osspriteop_put_sprite_user_coords(osspriteop_PTR,playground.area,playground.id,0,0,0);
for(i=0 ; i<XMAX ; i++)
for(j=0 ; j<YMAX ; j++)
osspriteop_put_sprite_user_coords(osspriteop_PTR,cover.area,cover.id,(XOFF+i*20)*2,(YOFF+j*10)*4,0);
/* ab hier wieder normal */
werr(xosspriteop_unswitch_output(s1,s2,s3,s4)) ;
free(old) ;
}
void draw_sprite(int x,int y,osspriteop_id help)
{wimp_draw r;
osspriteop_save_area *old;
int s1,s2,s3,s4;
old = (osspriteop_save_area *)malloc(osspriteop_read_save_area_size(osspriteop_PTR,my_sprite.area,my_sprite.id)) ;
old->a[0] = 0 ;
werr(xosspriteop_switch_output_to_sprite(osspriteop_PTR,my_sprite.area,my_sprite.id,old,&s1,&s2,&s3,&s4));
osspriteop_put_sprite_user_coords(osspriteop_PTR,cover.area,help,(XOFF+x*20)*2,(YOFF+y*10)*4,0);
werr(xosspriteop_unswitch_output(s1,s2,s3,s4)) ;
r.w = mainwindow.w ;
r.box.x0 = (x*20+XOFF)*2 ;
r.box.y0 = (10*y+YOFF-HEIGHT)*4 ;
r.box.x1 = ((x+1)*20+XOFF)*2 ;
r.box.y1 = (10*(y+1)-HEIGHT+YOFF)*4 ;
update_window(r) ;
free(old) ;
}
/* Mouse-Click verarbeiten */
void click_on(int x,int y,bits button)
{wimp_window_state wstate ;
osspriteop_id help ;
int x1,y1 ;
wstate.w = mainwindow.w ;
wimp_get_window_state(&wstate) ; /*Koordinatentransformation*/
x1 = (x+wstate.xscroll-wstate.visible.x0-XOFF*2)/2/20 ;
y1 = (y+wstate.yscroll-wstate.visible.y0-YOFF*4)/4/10 ;
if(x1>=0 && x1<XMAX && y1>=0 && y1<YMAX && mines_left != -1) /* Innerhalb und erlaubt ?*/
{ hint_choose_done(x1,y1,&button);
if( (button == wimp_CLICK_SELECT) && !(feld[x1][y1] & MARK) ) /* Ausknobeln was getan werden soll*/
{if(feld[x1][y1] & MINE) /* Links auf geschlossene Mine ? */
show_all() ; /* ja : leider verloren */
else if(feld[x1][y1] & CLOSED)
{open_field(x1,y1) ; /* sonst öffnen */
if(fields_left == 0) /* alle freien Felder gefunden ? */
win() ; /* dann war das der Sieg */
}
}
else if (button == wimp_CLICK_ADJUST) /* Rechte Taste */
{if( (feld[x1][y1] & CLOSED) && ( (mines_left > 0) || ((feld[x1][y1] & MARK) )) && (mines_left != -1))
{feld[x1][y1] ^= MARK ; /* Marke toggeln */
if(feld[x1][y1] & MARK)
{help = mark.id ;
mines_left-- ;
show_anzahl() ;
if ((mines_left==0) && no_error()) win();
}
else
{help = cover.id ;
mines_left++ ;
show_anzahl() ;
}
draw_sprite(x1,y1,help);
}
else
{if( !(feld[x1][y1] & CLOSED) )
{if(tidy_up(x1,y1))
show_all() ;
else
{if ((fields_left == 0) && no_error()) win();
}
}
}
}
}
}
int no_error(void)
{
int x,y;
for (x=0;x<XMAX;x++) for (y=0;y<YMAX;y++)
if (((feld[x][y] & MINE) && !(feld[x][y] & MARK)) ||
(!(feld[x][y] & MINE) && (feld[x][y] & MARK))) return FALSE;
return TRUE;
}
extern int unnecessary_hints;
/* eine sehr einfache Siegesroutiene */
void win(void)
{
wimp_window_state s;
wimp_icon *i=winwindow.window->icons ;
show_all();
sprintf(i[0].data.indirected_text.text,"Unnecessary hints : %3i",unnecessary_hints);
s.w = winwindow.w ;
wimp_get_window_state(&s);
s.next=wimp_TOP;
wimp_open_window((wimp_open*)&s);
}
/* aufräumen (Rechtsclick auf ein offenes Feld */
int tidy_up(int x,int y)
{int c = count(x,y) ;
int i,x1,y1 ;
for(i=0 ; i<8 ; i++)
{x1 = x+off[i][0] ;
y1 = y+off[i][1] ;
if(x1>=0 && x1<XMAX && y1>=0 && y1<YMAX)
if(feld[x1][y1] & MARK)
c-- ;
}
if(c==0)
{for(i=0 ; i<8 ; i++)
{x1 = x+off[i][0] ;
y1 = y+off[i][1] ;
if(x1>=0 && x1<XMAX && y1>=0 && y1<YMAX)
{if( feld[x1][y1] & CLOSED )
{if( !(feld[x1][y1] & MARK) )
{if( feld[x1][y1] & MINE)
return 1 ;
else
open_field(x1,y1) ;
}
}
}
}
}
return 0 ;
}
/* Ein Feld (gegebenenfalls mehrere rekursiv) oeffnen */
void open_field(int x,int y)
{int i ;
osspriteop_id help ;
if(x>=0 && x<XMAX && y>=0 && y<YMAX)
{help = open[i=count(x,y)].id ;
feld[x][y] &= ~CLOSED ;
fields_left-- ;
draw_sprite(x,y,help);
if(i == 0)
{for(i=0;i<8;i++)
if( (feld[x+off[i][0]][y+off[i][1]] & CLOSED)
&& !(feld[x+off[i][0]][y+off[i][1]] & MARK))
open_field(x+off[i][0],y+off[i][1]) ;
}
}
}
/* alles zeigen */
void show_all(void)
{int x,y,h,s1,s2,s3,s4 ;
wimp_draw r;
osspriteop_save_area *old ;
osspriteop_id help ;
mines_left = -1 ;
ShadeMenu(mainmenu,HINT) ;
old = (osspriteop_save_area *)malloc(osspriteop_read_save_area_size(osspriteop_PTR,my_sprite.area,my_sprite.id)) ;
old->a[0] = 0 ;
for(x=0 ; x<XMAX ; x++)
{werr(xosspriteop_switch_output_to_sprite(osspriteop_PTR,my_sprite.area,my_sprite.id,old,&s1,&s2,&s3,&s4));
for(y=0 ; y<YMAX ; y++)
{h = feld[x][y] ;
help = 0 ;
if(h & MINE)
{if( !(h & MARK) )
help = mine.id ;
}
else
{if( h & CLOSED )
{if(h & MARK)
help = nomine.id ;
else
help = open[count(x,y)].id ;
}
}
if(help)
osspriteop_put_sprite_user_coords(osspriteop_PTR,my_sprite.area,help,(XOFF+x*20)*2,(YOFF+y*10)*4,0);
}
werr(xosspriteop_unswitch_output(s1,s2,s3,s4)) ;
r.w = mainwindow.w ;
r.box.x0 = (x*20+XOFF)*2 ;
r.box.y0 = (-HEIGHT+YOFF)*4 ;
r.box.x1 = ((x+1)*20+XOFF)*2 ;
r.box.y1 = (10*YMAX-HEIGHT+YOFF)*4 ;
update_window(r) ;
}
show_anzahl() ;
free(old) ;
}
void blotch(void)
{int x,y,min,x1,y1;
int i,j,k,l,h ;
x = XMAX/2 ;
y = YMAX/2 ;
min = 9 ;
if(XMAX>YMAX)
j = x ;
else
j = y ;
if(x>=0 && x<XMAX && y>=0 && y<YMAX)
{if( !(feld[x][y] & MINE) && (feld[x][y] & CLOSED) )
{x1 = x ;
y1 = y ;
min = count(x,y) ;
}
}
x-- ;
y-- ;
for(i=1;i<=j;i++)
{for(l=0 ; l<4 ; l++)
{k = 2 * i + 1 ;
do{if(x>=0 && x<XMAX && y>=0 && y<YMAX)
{if( !(feld[x][y] & MINE) && (feld[x][y] & CLOSED) )
{h = count(x,y) ;
if(h<min)
{x1 = x ;
y1 = y ;
min = h ;
}
}
}
k-- ;
if(k>0)
{switch(l)
{case 0:
x++ ;
break ;
case 1:
y++ ;
break ;
case 2:
x-- ;
break ;
case 3:
y-- ;
break ;
}
}
}while(k>0) ;
}
x-- ;
y-- ;
}
if(min!=9) open_field(x1,y1) ;
}
/* Anzahl der Minen minus Anzahl der Markierungen anzeigen */
void show_anzahl(void)
{int flag = 0,h2 = mines_left,h1 = 1000,h3,i,s1,s2,s3,s4 ;
osspriteop_save_area *old ;
osspriteop_id shelp ;
wimp_draw r;
if(h2<0) h2 = 0 ;
old = (osspriteop_save_area *)malloc(osspriteop_read_save_area_size(osspriteop_PTR,my_sprite.area,my_sprite.id)) ;
old->a[0] = 0 ;
osspriteop_switch_output_to_sprite(osspriteop_PTR,my_sprite.area,my_sprite.id,old,&s1,&s2,&s3,&s4);
for(i=0;i<4;i++)
{h3 = h2/h1 ;
if(h3 == 0)
{if((flag == 1) || (i == 3) )
shelp = number[1].id ;
else
shelp = number[0].id ;
}
else
{shelp = number[h3+1].id ;
flag = 1 ;
}
osspriteop_put_sprite_user_coords(osspriteop_PTR,my_sprite.area,shelp,(110+i*10)*2,(HEIGHT-10-9)*4,0);
h2 -= h1*h3 ;
h1 /= 10 ;
}
osspriteop_unswitch_output(s1,s2,s3,s4) ;
r.w = mainwindow.w ;
r.box.x0 = 110*2 ;
r.box.y0 = ((HEIGHT-10-9)-HEIGHT)*4 ;
r.box.x1 = (110+4*10)*2 ;
r.box.y1 = ((HEIGHT-8)-HEIGHT)*4 ;
update_window(r) ;
free(old) ;
}
/* Anzahl der Minen um ein Feld zählen */
int count(int x,int y)
{int i,c=0,x1,y1 ;
for(i=0 ; i<8 ; i++)
{x1 = x+off[i][0] ;
y1 = y+off[i][1] ;
if(x1>=0 && x1<XMAX && y1>=0 && y1<YMAX)
if(feld[x1][y1] & MINE)
c++ ;
}
return c ;
}
/* Windows und Dialogboxen */
void update_window(wimp_draw r)
{ wimp_draw new_r = r;
bool more;
more = wimp_update_window(&new_r);
if(more)
redo_window(new_r,more);
}
void redo_window(wimp_draw r, BOOL more)
{bool more_to_do = more;
wimp_draw new_r = r;
os_factors factors;
/* --- ask how the WIMP is going to scale our sprite --- */
wimp_read_pix_trans(osspriteop_PTR,my_sprite.area, my_sprite.id, &factors, trans);
/* --- refresh the window's contents --- */
while (more_to_do)
{osspriteop_put_sprite_scaled(osspriteop_PTR,my_sprite.area,my_sprite.id,r.box.x0,r.box.y0,0,&factors,trans);
more_to_do = wimp_get_rectangle(&new_r);
}
}
void redraw_window(wimp_w handle)
{BOOL more;
wimp_draw r;
wimp_window_info winfo;
winfo.w = handle;
wimp_get_window_info(&winfo);
/* --- do the redraw --- */
r.w = handle;
more = wimp_redraw_window(&r);
if (more)
redo_window(r, more);
}
void open_window(wimp_open *o)
{static int old_x, old_y;
o->xscroll = 0; /* --- force scroll offsets to 0, since the window always --- */
o->yscroll = 0; /* --- represents the whole display --- */
wimp_open_window(o);
/* --- only do a redraw if the size of the window has changed --- */
if (old_x != (o->visible.x1 - o->visible.x0) || old_y != (o->visible.y1 - o->visible.y0))
{
redraw_window(o->w);
old_x = o->visible.x1 - o->visible.x0;
old_y = o->visible.y1 - o->visible.y0;
}
}
void icon_links()
{wimp_window_state state;
if(mines_left == -10000 )
{res_feld();
res_sprite() ;
UnshadeMenu(mainmenu,HINT) ;
show_anzahl() ;
blotch() ;
}
/* --- open the window we created --- */
state.w = mainwindow.w ;
wimp_get_window_state(&state) ;
state.next = wimp_TOP; /* make sure it is opened in front */
open_window((wimp_open*)&state);
displaying = TRUE;
}
/* event handler */
void new_game(void)
{ wimp_window_state state;
close_win_window();
res_feld() ;
res_sprite() ;
hint_new_game();
show_anzahl();
if(displaying) wimp_force_redraw(mainwindow.w,0,-HEIGHT*4,WEIDTH*2,0);
else
{state.w = mainwindow.w ;
wimp_get_window_state(&state) ;
state.next = wimp_TOP; /* make sure it is opened in front */
open_window((wimp_open*)&state);
if(displaying == FALSE)
{wimp_force_redraw(mainwindow.w,0,-HEIGHT*4,WEIDTH*2,0);
displaying = TRUE;
}
}
blotch() ;
UnshadeMenu(mainmenu,HINT);
}
void end_game(void)
{
close_win_window();
hint_exit();
exit(0) ;
}
/* ********************* Fenster action ********************* */
#define PollMask wimp_MASK_NULL|wimp_MASK_LEAVING|wimp_MASK_ENTERING|wimp_MASK_LOSE|wimp_MASK_GAIN
void mine_handler()
{wimp_message reply ;
wimp_pointer pointer,*ppoint ;
int event,pw = 0 ;
static int mx,my ;
wimp_block b ;
while(1)
{werr(xwimp_poll(PollMask,&b,&pw,&event)) ;
switch(event)
{case wimp_REDRAW_WINDOW_REQUEST:
if(b.redraw.w == mainwindow.w) redraw_window(b.redraw.w);
break;
case wimp_OPEN_WINDOW_REQUEST:
if(b.open.w == mainwindow.w) open_window(&b.open);
else wimp_open_window(&b.open);
break;
case wimp_CLOSE_WINDOW_REQUEST:
if(b.close.w == mainwindow.w)
{wimp_close_window(b.close.w);
displaying = FALSE;
}
else if(b.close.w == winwindow.w) wimp_close_window(b.close.w);
break;
case wimp_MOUSE_CLICK: /* mouse buttons */
if(b.pointer.buttons == wimp_CLICK_MENU)
{mx = b.pointer.pos.x-75 ;
if(b.pointer.w == wimp_ICON_BAR)
werr(xwimp_create_menu((wimp_menu*)&mainmenu,mx,my=528)) ;
else
werr(xwimp_create_menu((wimp_menu*)&mainmenu,mx,my=b.pointer.pos.y)) ;
}
else if(b.pointer.w == mainwindow.w ) /* mein Fenster ? */
{if(mines_left != -1 ) click_on(b.pointer.pos.x,b.pointer.pos.y,b.pointer.buttons) ;
}
else if(b.pointer.i == baricon) icon_links() ;
else if(b.pointer.w == winwindow.w)
{if(b.pointer.buttons & wimp_CLICK_MENU) wimp_close_window(winwindow.w);
else if (b.pointer.i==1)
{wimp_close_window(winwindow.w);
new_game();
}
else if (b.pointer.i==2)
{wimp_close_window(winwindow.w);
end_game();
}
}
else hint_click(&b) ;
break;
case wimp_MENU_SELECTION:
werr(xwimp_get_pointer_info(&pointer));
switch(b.selection.items[0])
{case LEV1 :
case LEV2 :
case LEV3 :
case LEV4 :
UntickMenu(mainmenu,level+LEV1);
level = b.selection.items[0] - LEV1 ;
TickMenu(mainmenu,level+LEV1) ;
break ;
case CUSTOM :
if(b.selection.items[1] == 1 )
{UntickMenu(mainmenu,level+LEV1) ;
level = b.selection.items[0] - LEV1 ;
TickMenu(mainmenu,level+LEV1) ;
}
break ;
case NEU :
new_game();
break ;
case HINT :
hint_it() ;
break ;
case QUIT :
end_game();
break ;
}
if(pointer.buttons == wimp_CLICK_ADJUST)
werr(xwimp_create_menu((wimp_menu*)&mainmenu,mx,my)) ;
break;
case wimp_USER_MESSAGE:
case wimp_USER_MESSAGE_RECORDED: /* check for broadcast */
switch(b.message.action)
{ case message_QUIT:
exit(0) ;
break ;
case message_PALETTE_CHANGE: /* einfach neue Pallette anpassen */
colourtrans_select_table((os_mode)MODE,0,(os_mode)-1,(os_palette *)-1,(osspriteop_trans_tab *)trans);
break;
case message_MODE_CHANGE: /* auch bei Mode-change */
colourtrans_select_table((os_mode)MODE,0,(os_mode)-1,(os_palette *)-1,(osspriteop_trans_tab *)trans);
break;
case 0x502: /* Help Request */
reply.your_ref = b.message.my_ref;
reply.action = 0x503;
reply.size = 256;
ppoint = (wimp_pointer*)&(b.message.data.reserved[0]) ;
if(ppoint->w == mainwindow.w) /*ie. window*/
sprintf((char*)reply.data.reserved,
"SELECT on covered field opens (what ever will happen)|MADJUST on covered field toggles mark|MADJUST on open field opens known surroundings");
else if ((ppoint->w == wimp_ICON_BAR) && (ppoint->i == baricon))
sprintf((char *)reply.data.reserved,"Click SELECT to start displaying the game !");
else
sprintf((char*)reply.data.reserved,
"Mines is a tiny little testing application.|MThe rules could be found in the !Help file");
wimp_send_message(wimp_USER_MESSAGE,&reply,b.message.sender);
break;
default:
break;
}
default: /* we're not interested in any other events */
break;
}
}
}
void close_win_window(void)
{
wimp_window_state s;
s.w = winwindow.w ;
wimp_get_window_state(&s);
if (s.flags & wimp_WINDOW_OPEN) wimp_close_window(winwindow.w);
}
/* alles instalieren */
void create_sprite(void)
{int i;
osspriteop_area *a;
FILE *f ;
os_error error1 = {0,"Fatal error - failed to allocate store for sprite"};
if((my_sprite.area = (osspriteop_area*)malloc(SpriteSize)) == 0) /*allocate our own sprite area to hold display */
{wimp_report_error(&error1,1,"Mines");
exit(1) ;
}
my_sprite.area->size = SpriteSize ;
my_sprite.area->first = 16 ;
osspriteop_clear_sprites(osspriteop_PTR,my_sprite.area);
osspriteop_create_sprite(osspriteop_NAME,my_sprite.area,"minedisplay",0,X_SIZE,Y_SIZE,(os_mode)MODE);
my_sprite.id = (osspriteop_id)osspriteop_select_sprite(osspriteop_NAME,my_sprite.area,(osspriteop_id)"minedisplay");
f=fopen("<Mines$Dir>.Sprites","r") ;
fseek(f,0,SEEK_END) ;
i = (int)ftell(f) ;
a = (osspriteop_area*)malloc(i+4) ;
fseek(f,0,SEEK_SET) ;
fread(((char*)a)+4,1,i,f) ;
fclose(f) ;
a->size = i+4 ;
playground.area=mark.area=cover.area=mine.area=nomine.area=questionmark.area=barsprite.area=a ;
playground.id = (osspriteop_id)osspriteop_select_sprite(osspriteop_NAME,a,(osspriteop_id)"playground");
mark.id = (osspriteop_id)osspriteop_select_sprite(osspriteop_NAME,a,(osspriteop_id)"mark");
cover.id = (osspriteop_id)osspriteop_select_sprite(osspriteop_NAME,a,(osspriteop_id)"cover");
mine.id = (osspriteop_id)osspriteop_select_sprite(osspriteop_NAME,a,(osspriteop_id)"mine");
nomine.id = (osspriteop_id)osspriteop_select_sprite(osspriteop_NAME,a,(osspriteop_id)"nomine");
for(i=0 ; i<9 ; i++)
{open[i].area = a ;
open[i].id = (osspriteop_id)osspriteop_select_sprite(osspriteop_NAME,a,(osspriteop_id)openstr[i]);
}
for(i=0 ; i<11 ; i++)
{number[i].area=a ;
number[i].id = (osspriteop_id)osspriteop_select_sprite(osspriteop_NAME,a,(osspriteop_id)numberstr[i]);
}
questionmark.id = (osspriteop_id)osspriteop_select_sprite(osspriteop_NAME,a,(osspriteop_id)"questionmark");
barsprite.id = (osspriteop_id)osspriteop_select_sprite(osspriteop_NAME,a,(osspriteop_id)"barmines");
}
BOOL main_init(void)
{wimp_icon_create bari;
osword_timer_block clock ;
wimp_MESSAGE_LIST(10) ml ;
ml.messages[0] = message_PALETTE_CHANGE ;
ml.messages[1] = message_MODE_CHANGE ;
ml.messages[2] = 0x502 ;
ml.messages[3] = 0 ;
I_am = wimp_initialise(310,"Mines",(wimp_message_list*)&ml,&This_Version) ;
create_sprite() ; /* sprites erzeugen b.z.w suchen */
werr(xwimp_open_template("<Mines$Dir>.Templates")); /* her mit den Templates !*/
trans = (osspriteop_trans_tab *)malloc(16) ;
loadwin(&mainwindow,"MainWindow") ;/* Mainwindow suchen */
werr(xwimp_create_window( mainwindow.window , &(mainwindow.w) )) ;/* Hauptfenster erzeugen */
/* und Fehler abfangen */
loadwin(&winwindow,"Win") ;
werr(xwimp_create_window( winwindow.window , &(winwindow.w) )) ;
loadwin(&infowindow,"ProgInfo") ;
strncpy(infowindow.window->icons[9].data.indirected_text.text,VERSION,infowindow.window->icons[4].data.indirected_text.size-1);
werr(xwimp_create_window(infowindow.window,&infowindow.w));
mainmenu.entries[0].sub_menu = (wimp_menu*)infowindow.w ;
if (!hint_init()) return FALSE;
cusmenu.entries[0].data.indirected_text.text = cusstr ;
cusmenu.entries[0].data.indirected_text.validation = "a0-9" ;
cusmenu.entries[0].data.indirected_text.size = 4 ;
baricon = 0 ;
bari.w = wimp_ICON_BAR_RIGHT ;
bari.icon.extent.x0 = 0 ;
bari.icon.extent.y0 = 0 ;
bari.icon.extent.x1 = 78 ;
bari.icon.extent.y1 = 68 ;
bari.icon.flags = 0x0000311a ;
bari.icon.data.indirected_sprite.id = barsprite.id ;
bari.icon.data.indirected_sprite.area = barsprite.area ;
bari.icon.data.indirected_sprite.size = 0 ;
werr(xwimp_create_icon(&bari,&baricon)) ; /* Icon auf die Bar */
TickMenu(mainmenu,LEV1) ;
ShadeMenu(mainmenu,HINT) ;
wimp_close_template();
/* --- read the palette --- */
werr(xcolourtrans_select_table((os_mode)MODE,0,(os_mode)-1,(os_palette *)-1,(osspriteop_trans_tab *)trans));
mines_left = -10000 ; /* Flag für erstes mal */
werr(xosword_read_system_clock(&clock));
srand(clock.b[0]|clock.b[1]<<8|clock.b[2]<<16|clock.b[3]<<24 | 1) ;
return TRUE ; /* hat alles geklappt */
}
/* Hauptprogramm */
int main()
{if(main_init()) mine_handler() ;
return 0 ;
}